iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 8
0

題目:

https://leetcode.com/problems/powx-n/
計算平方數值。

解題思路:

利用遞迴概念求出平方值。

C版本:

double myPow(double x, int n) {
    if (n == 0) return 1;
        double half = myPow(x, n / 2);
        if (n % 2 == 0) return half * half;
        else if (n > 0) return half * half * x;
        else return half * half / x;
}

Javascript版本:

var myPow = function(x, n) {
    if (n == 0) return 1;
    var half = myPow(x, parseInt(n / 2));
    if (n % 2 == 0) 
    {    
        return half * half;
    }
    else if (n > 0) 
    {   
        return half * half * x;
    }
    else return 
    {
        half * half / x;
    }
};

程式Github分享:

https://github.com/SIAOYUCHEN/leetcode

相似主題分享:

https://ithelp.ithome.com.tw/users/20100009/ironman/2500
https://ithelp.ithome.com.tw/users/20113393/ironman/2169
https://ithelp.ithome.com.tw/users/20107480/ironman/2435
https://ithelp.ithome.com.tw/users/20107195/ironman/2382
https://ithelp.ithome.com.tw/users/20119871/ironman/2210
https://ithelp.ithome.com.tw/users/20106426/ironman/2136

本日分享:

You can’t have all that you want, but you can give it all you have got.
你不能樣樣順利,但可以事事盡力


上一篇
DAY7 Search Insert Position
下一篇
DAY9 Plus One
系列文
刷題記錄與人生分享 共 34 篇
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言